<?php

namespace app\admin\service\setting;

use app\common\model\setting\ConfigPay;
use tmcore\base\BaseService;
use tmcore\services\ConfigService;

class PayService extends BaseService
{
    /**
     * 支付设置列表
     * @return array
     */
    public static function lists()
    {
        // 搜索条件
        $searchParam = [
            '=' => ['status'],
        ];
        $where = self::getSearchData($searchParam);

        return ConfigPay::where($where)
            ->field('id,name,identify,status')
            ->order(['id' => 'asc'])
            ->select()
            ->toArray();
    }

    /**
     * 支付设置详情
     * @param $params
     * @return mixed
     */
    public static function detail($params)
    {
        return ConfigPay::where(['identify' => $params['identify']])->findOrEmpty()->toArray();
    }

    /**
     * 设置支付参数
     * @param $params
     * @return bool
     */
    public static function edit($params)
    {
        self::beginTrans();
        try {
            switch ($params['identify']) {
                case 'cash': //线下支付
                    $params['params'] = [];
                    break;
                case 'balance': //余额支付
                    $params['params'] = [];
                    break;
                case 'wechat': //微信支付
                    $params['params'] = [
                        'mch_id' => $params['mch_id'] ?? '', //商户号
                        'mch_secret_key' => $params['mch_secret_key'] ?? '', //v3商户私钥
                        'mch_secret_key_v2' => $params['mch_secret_key_v2'] ?? '', //v2商户私钥
                        'mch_secret_cert' => $params['mch_secret_cert'] ?? '', //商户私钥证书 apiclient_key.pem
                        'mch_public_cert_path' => $params['mch_public_cert_path'] ?? '', //商户公钥证书 apiclient_cert.pem
                    ];
                    break;
                case 'alipay': //支付宝支付
                    $params['params'] = [
                        'app_id' => $params['app_id'] ?? '', // 应用appid
                        'app_secret_cert' => $params['app_secret_cert'] ?? '', //应用私钥
                        'app_public_cert_path' => $params['app_public_cert_path'] ?? '', //应用公钥证书
                        'alipay_public_cert_path' => $params['alipay_public_cert_path'] ?? '', //支付宝公钥证书
                        'alipay_root_cert_path' => $params['alipay_root_cert_path'] ?? '' //支付宝根证书
                    ];
                    break;
                default:
                    $params['params'] = [];
                    break;
            }

            ConfigPay::update([
                'icon' => $params['icon'],
                'status' => $params['status'],
                'params' => $params['params'],
            ],['identify' => $params['identify']]);

            self::commitTrans();
            // 刷新缓存
            (new ConfigService)->setConfig('create', 'pay', $params['identify']);
            return true;
        } catch (\Exception $e) {
            self::rollbackTrans();
            return self::setError($e->getMessage());
        }
    }

    /**
     * 切换状态
     * @param $params
     * @return bool
     */
    public static function change($params)
    {
        self::beginTrans();
        try {
            if(isset($params['cash_status'])) ConfigPay::update(['status' => $params['cash_status']],['identify' => 'cash']);
            if(isset($params['balance_status'])) ConfigPay::update(['status' => $params['balance_status']],['identify' => 'balance']);
            if(isset($params['wechat_status'])) ConfigPay::update(['status' => $params['wechat_status']],['identify' => 'wechat']);
            if(isset($params['alipay_status'])) ConfigPay::update(['status' => $params['alipay_status']],['identify' => 'alipay']);
            self::commitTrans();
            return true;
        } catch (\Exception $e) {
            self::rollbackTrans();
            return self::setError($e->getMessage());
        }
    }
}